using System;
using System.Collections.Generic;
using System.Diagnostics;
using Godot;
using Godot.Collections;

public partial class Enemy:CharacterBody2D
{
    [Export]
    public int Health = 10;
    
    [Export]
    public float Speed = 300.0f,JumpVelocity = -600.0f;
	
    public Vector2 ScreenSize;

    public Player player;
    
    public AudioStreamPlayer2D audioStreamPlayer2D;

    public List<AudioStream> audioStreams;

    public bool FacingLeft = true;

    public bool enableGravity = true;

    public StateMeachine stateMeachine;

    public override void _Ready()
    {
        ScreenSize = GetViewportRect().Size;

        player = GetNode<Player>("/root/Main/Player");

        audioStreamPlayer2D = GetNode<AudioStreamPlayer2D>("HurtMusic");

        audioStreams = new(){
            ResourceLoader.Load<AudioStream>("res://resources/audio/enemy_hurt_1.mp3"),
            ResourceLoader.Load<AudioStream>("res://resources/audio/enemy_hurt_2.mp3"),
            ResourceLoader.Load<AudioStream>("res://resources/audio/enemy_hurt_3.mp3")
        };

        stateMeachine = new StateMeachine(GetNode<StateNode>("StateNode/Idle"));
    }
    
    public void UpdateFace()
    {
        FacingLeft = player.Position.X<Position.X;
    }

    public void OnHurt()
    {
        audioStreamPlayer2D.Stream = audioStreams[(int)MyUtil.RangeRandom(0,3)];
        audioStreamPlayer2D.Play();

        Health-=1;
    }

    public void SwitchTo(string type)
    {
        UpdateFace();
        stateMeachine.SwitchTo(GetNode<StateNode>($"StateNode/{type}"));
    }

    public override void _PhysicsProcess(double delta)
    {
        float scale = TimeController.Instance.GetTimeScale(this);
		// Add the gravity.
		if (!IsOnFloor()&&enableGravity)
		{
			Velocity += GetGravity() * (float)delta;
		}				

		delta *=scale;		
		
        stateMeachine.OnUpdate((float)delta);

		// var collision = MoveAndCollide(Velocity*(float)delta);
        // if(collision != null)
        // {
        //     var collider = collision.GetCollider() as CollisionObject2D;

        //     if(collider.Name == "Player")
        //     {
        //         GD.Print(this.Name+" collide player");
        //         player.OnHurt();
        //     }
        // }

        Vector2 velocity = Velocity;

        MoveAndSlide();
        for (int i = 0; i < GetSlideCollisionCount(); i++)
        {
            KinematicCollision2D collision = GetSlideCollision(i);
            var collider = collision.GetCollider() as CollisionObject2D;
            if(collider.Name == "Player")
            {
                GD.Print(this.Name+" collide player");
                player.OnHurt();
                Velocity = velocity;
            }
        }

		Position = new Vector2(
			x:Mathf.Clamp(Position.X,0,ScreenSize.X),
			y:Mathf.Clamp(Position.Y,0,ScreenSize.Y)
		);

        var animation = GetNode<AnimatedSprite2D>("AnimaEnemy");
		if(FacingLeft)
			animation.FlipH = false;
		else
			animation.FlipH = true;        
		animation.Play();
    }
}